/**
 * 这个JS是给phantomjs使用的
 * Created by Administrator on 2017/6/11.
 */
"use strict";
phantom.cookiesEnabled = true

if ( typeof(phantom) !== "undefined" ) {
    var page = require('webpage').create(),
        system = require('system');

    if (system.args.length < 2) {
        // console.log('Usage: loadurlwithoutcss.js URL');
        phantom.exit();
    }

    var address = system.args[1];
    var pwd = system.args[2]

    page.onResourceRequested = function(requestData, request) {
        if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || requestData.headers['Content-Type'] == 'text/css') {
            // console.log('The url of the request is matching. Aborting: ' + requestData['url']);
            request.abort();
        }
    };

    page.settings.loadImages = false;  //为了提升加载速度，不加载图片
    page.settings.resourceTimeout = 10000;//超过10秒放弃加载


    page.open(address, function(status) {
        if (status === 'success') {
            var form = page.evaluate(function(pwd){
                var publicKey = document.getElementById("pubKey").value
                return {
                    uuid          : document.getElementById("uuid").value,
                    eid           : document.getElementById("eid").value,
                    fp            : document.getElementById("sessionId").value,
                    pubKey        : publicKey,
                    token         : document.getElementById("token").value,
                    sa_token      : document.getElementById("sa_token").value,
                    _t            : document.getElementById("token").value,
                    loginType     : document.getElementById("loginType").value,
                    nloginpwd     : function(){
                        var encrypt = new JSEncrypt();
                        encrypt.setPublicKey(publicKey);
                        return encrypt.encrypt(pwd);
                    }(),
                    authCodeUrl   : document.getElementById("JD_Verification1").getAttribute("src2"),
                    seqSid        : String(window._jdtdmap_sessionId),
                    localStorage  : function(){
                        var localStorageArray = []
                        for(var i=0;i<localStorage.length;i++){
                            localStorageArray.push({
                                key : localStorage.key(i),
                                value : localStorage.getItem(localStorage.key(i))
                            });
                        }
                        return localStorageArray;
                    }()
                }
            }, pwd);
            // console.log(form.eid);
            // console.log(form.fp);
            // console.log(form.pubKey);
            // console.log(form.token);
            // console.log(form.sa_token);
            // console.log(form._t);
            // console.log(form.loginType)
            // console.log(form.nloginpwd);
            form.cookies = phantom.cookies
            console.log(JSON.stringify(form))
            phantom.exit();
        } else {
            // console.log('Unable to load the address!');
            phantom.exit();
        }
    });
} else {
    alert("* Script running in the Page context.");
}